home *** CD-ROM | disk | FTP | other *** search
- /*
- File: THINKTranslationAppShell.c
-
- Contains: Application shell for creating a Translation Extension in THINK C.
-
- Copyright: © 1992 by Apple Computer, Inc., all rights reserved.
-
- Overview:
- - register our translation extension as a component
- - create a temp file (in the preferences folder) and get FSSpec
- - call StandardGetFile to obtain an FSSpec for the file to be translated
- - call ExecuteTranslation with these two FSSpecs
- - voila! the translation is executed via our translation extension calls
- (debuggable in Think C!)
-
- What you have to do to use it:
- - add your translation extension source to the project and compile
- - modify the 'thng' resource in THINKTranslationAppShell.π.rsrc to have your manufacturer signature
- - add your translation extension's resources to THINKTranslationAppShell.π.rsrc
- - to do a file translation:
- - select a file to translate
- - select a destination format
- - run the translation, you will be able to debug your file
- translation routines in the Think C debugger
- - to do a scrap translation (to 'styl' only supported now)
- - twitch to source app, choose Copy
- - twitch to this app, choose "Do 'styl' Translation"
- - twitch to destination app, choose Paste
-
- */
-
- #ifndef THINK_C
- #include <Types.h>
- #include <Quickdraw.h>
- #include <Scrap.h>
- #include <StandardFile.h>
- #include <Menus.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <Fonts.h>
- #include <OSEvents.h>
- #endif
- #include <Folders.h>
- #include <Script.h>
-
- #include "TranslationExtensions.h"
- #include "Components.h"
-
-
- #define appleID 128 /* resource IDs of menus */
- #define testID 129
-
- #define appleM 0 /* menu indices */
- #define testM 1
-
- #define setSourceCmd 1
- #define destFormatCmd 2
- #define translateFileCmd 3
- #define translateScrapCmd 5
- #define quitCmd 7
-
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // ExecuteTranslation
- //
- // This routine allows the developer to use THINK C to debug a File Translation Extension.
- // It executes something very similar to a desktop translator.
- //
- // Enter: theDocument If theDocument is NULL, this routine will display and
- // run the Desktop Translator dialog (storing the setting in the
- // preference file (described next). If theDocument is not NUILL,
- // then theDocument will be translated into the format specified in
- // the preference file. The new document will be created in the
- // same directory as theDocument.
- // thePreferenceFile An FSSpec describing where and what the name of the preference
- // file. The file should already been created (and have a resource
- // fork) prior to making this call. It's also important that the
- // preference file is closed prior to this call.
- //
- // Exit: returns Any errors that might occur
- //
- // ••• IMPORTANT : DISCLAMER •••
- //
- // THE TRAP EXECUTETRANSLATION() IS NOT FULLY SUPPORTED BY APPLE COMPUTER, INC. THIS TRAP
- // IS PROVIDED ON A "TEMPORARY" BASIS AND WILL BE REMOVED IN THE FUTURE. YOU SHOULD THEREFORE
- // NOT USE THIS TRAP IN ANY WAY EXCEPT WITHIN THIS SHELL.
- //
- pascal OSErr ExecuteTranslation(const FSSpec* theDocument,
- const FSSpec* thePreferencesFile)
- = {0x7028,0xABFC};
-
-
- // globals
- Component gComponent;
- Boolean gSourceDefined = false;
- FSSpec gSourceFile; // source document to be translated
- FSSpec gPrefFile; // file containing information on what format to translate to
- MenuHandle gMenus[2];
-
-
-
-
- /////////////////////////////////////////////////////////////////////////////////////////
- //
- // This is the magic glue that is linked to the beginning of a Translation Extenstion
- // and interfaces to the ComponentMgr. When used in the THINK C environment,
- // a pointer to this routine is passed to RegisterComponent.
- //
- //
- pascal ComponentResult TranslateEntry( ComponentParameters *params, Handle storage )
- {
- ComponentRoutine theRtn;
-
- switch (params->what)
- {
- case kComponentOpenSelect:
- {
- ComponentInstance self = (ComponentInstance)params->params[0];
- Handle h = NewHandle(sizeof(ComponentInstance)); // allocate storage
-
- if ( h != NULL )
- {
- (**(ComponentInstance**)h) = self; // put instance in storage
- SetComponentInstanceStorage(self, h);
- return noErr;
- }
- else
- return MemError();
- };
- case kComponentCloseSelect:
- {
- DisposeHandle(storage);
- return noErr;
- };
- case kComponentCanDoSelect:
- {
- short selector = *(short*)params->params;
-
- return ( ((kComponentVersionSelect <= selector) && (selector <= kComponentOpenSelect))
- || ((kTranslateGetFileTranslationList <= selector) && (selector <= kTranslateTranslateFile))
- || ((kTranslateGetScrapTranslationList <= selector) && (selector <= kTranslateTranslateScrap)) );
- };
- case kComponentVersionSelect:
- return noErr;
-
- case kTranslateGetFileTranslationList: theRtn = (ComponentRoutine) DoGetFileTranslationList; break;
- case kTranslateIdentifyFile: theRtn = (ComponentRoutine) DoIdentifyFile; break;
- case kTranslateTranslateFile: theRtn = (ComponentRoutine) DoTranslateFile; break;
-
- case kTranslateGetScrapTranslationList: theRtn = (ComponentRoutine) DoGetScrapTranslationList; break;
- case kTranslateIdentifyScrap: theRtn = (ComponentRoutine) DoIdentifyScrap; break;
- case kTranslateTranslateScrap: theRtn = (ComponentRoutine) DoTranslateScrap; break;
- default: return (badComponentSelector);
- };
- return ( CallComponentFunctionWithStorage((Handle)**(ComponentInstance**)storage, params, theRtn) );
- }
-
-
- OSErr DoCommand(long mResult)
- {
- OSErr err;
- int theItem;
- Str255 name;
- StandardFileReply theReply;
-
-
- theItem = LoWord(mResult);
- switch (HiWord(mResult)) {
- case appleID:
- GetItem(gMenus[appleM], theItem, &name);
- OpenDeskAcc(name);
- break;
-
- case testID:
- switch (theItem) {
-
- // specify the file to be translated
- case setSourceCmd:
- StandardGetFile(NULL, -1, NULL, &theReply);
- if (theReply.sfGood)
- {
- gSourceFile = theReply.sfFile;
- gSourceDefined = true;
- }
- break;
-
- // specify the desired type of the output file
- case destFormatCmd:
- if ((err = ExecuteTranslation(0, &gPrefFile)) != noErr)
- return err;
- break;
-
- case translateFileCmd:
- // if there is a source file specified, translate normally
- if (gSourceDefined)
- {
- if ((err = ExecuteTranslation(&gSourceFile, &gPrefFile)) != noErr)
- return err;
- }
- break;
-
- case translateScrapCmd:
- {
- long offset;
-
- GetScrap(NULL, 'styl', &offset);
- break;
- }
-
- case quitCmd:
- return -1;
- }
- }
- HiliteMenu(0);
- return (noErr);
- }
-
-
- OSErr DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
- {
- switch (windowPart) {
- case inMenuBar:
- {
- if (gSourceDefined)
- EnableItem(gMenus[testM],translateFileCmd);
- else
- DisableItem(gMenus[testM],translateFileCmd);
- return(DoCommand(MenuSelect(myEvent->where)));
- }
- case inSysWindow:
- SystemClick(myEvent, whichWindow);
- break;
- }
- }
-
-
- void SetUpPreferenceFile(void)
- {
- // get pref file name
- FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &gPrefFile.vRefNum, &gPrefFile.parID);
- BlockMove("\pTHINKTransShell Pref", gPrefFile.name, 40);
-
- // create it (it may already exist, but that is OK)
- FSpCreateResFile(&gPrefFile, 'tran', '????', smSystemScript);
- }
-
-
- void SetUpMenus(void)
- {
- int i;
-
- gMenus[appleM] = NewMenu(appleID, "\p\024");
- AddResMenu(gMenus[appleM], 'DRVR');
- gMenus[testM] = GetMenu(testID);
- for ((i=appleM); (i<=testM); i++)
- InsertMenu(gMenus[i], 0) ;
- DrawMenuBar();
- }
-
-
- RegisterMyComponent()
- {
- ComponentDescription cd;
- ResourceSpec nameRes;
- Handle componentNameH;
- ComponentResourceHandle componentThngHdl;
-
- // this application must have a 'thng' resource 128
- componentThngHdl = (ComponentResourceHandle)GetResource('thng', 128);
- nameRes = (**componentThngHdl).componentName;
- componentNameH = GetResource(nameRes.resType, nameRes.resId);
-
- BlockMove((*componentThngHdl), &cd, sizeof(ComponentDescription));
- ReleaseResource((Handle)componentThngHdl);
-
- // Register the component, including its main entry point and its name
- gComponent = RegisterComponent(&cd, &TranslateEntry, false, componentNameH, NULL, NULL);
- }
-
-
- OSErr MainEvent(void)
- {
- EventRecord myEvent;
- WindowPtr whichWindow;
- short windowPart;
-
- SystemTask();
- if (GetNextEvent(everyEvent, &myEvent)) {
- switch (myEvent.what) {
- case mouseDown:
- windowPart = FindWindow(myEvent.where, &whichWindow);
- return (DoMouseDown(windowPart, whichWindow, &myEvent));
- break;
- case keyDown:
- case autoKey:
- {
- register char theChar;
-
- theChar = myEvent.message & charCodeMask;
- if ((myEvent.modifiers & cmdKey) != 0)
- {
- SysBeep(1);
- return(DoCommand(MenuKey( theChar)));
- }
- break;
- }
- } /* end of case myEvent.what */
- } /* if */
- return(noErr);
- }
-
- main()
- {
- #ifdef THINK_C
- InitGraf(&thePort); // the usual initializations...
- #else
- InitGraf(&qd.thePort);
- #endif
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- InitDialogs(0L);
- InitCursor();
- MaxApplZone();
- SetUpMenus();
-
- SetUpPreferenceFile();
-
- RegisterMyComponent();
-
- while (MainEvent() == noErr) ; // ... go to it
-
- UnregisterComponent(gComponent);
- }
-